# -*- coding: utf-8 -*-
"""
Created on Wed Jun 24 02:49:55 2020

@author: Farman
"""

import ClassificationSamples.SampleMan as sampleman
import WRS.WRSMan as wrsman
import Landsat.LandsatFileListMan as landsatfilesman
import GeotiffMan
import utm
import joblib

sample_man = sampleman.ClassificationSamples('/public/home/yule/code/team-w/ClassificationSamples/SamplesFromMrYu.joblib')
wrs_man = wrsman.WRS('/public/home/yule/code/team-w/WRS/wrs_control_points.joblib')
landsat_man = landsatfilesman.LandsatFilesMan('/public/home/yule/code/team-w/WorkDir/landsat_files.joblib')

# static sample points distribution, checked, all sample have corresponding Landsat images.
sample_id_pos_list = sample_man.list_id_pos_level

dict_path_row_id = {}

count = 0

for [pos_id, lon, lat, level_1, level_2] in sample_id_pos_list:
    path_row_list = wrs_man.get_path_row(lon, lat)

    for [path, row] in path_row_list:
        landsat_files = landsat_man.get_files_of_path_row(path, row)

        # skip path row pair without landsat scenes.
        if landsat_files == None:
            continue

        dict_row_id = dict_path_row_id.get(path)
        
        if dict_row_id == None:
            dict_row_id = {}
            list_id     = []
        else:
            list_id = dict_row_id.get(row)
            
            if list_id == None:
                list_id = []
        
        if list_id.count(pos_id) == 0:
            list_id.append(pos_id)
            dict_row_id.update({row : list_id})
            dict_path_row_id.update({path : dict_row_id})

    if count % 100 == 0:
        print(count)
    
    count += 1

joblib.dump(dict_path_row_id, 'WorkDir/samples_path_row_id.joblib')